/*-------------------------------------------------------------------------------
* file:     app_flash_log.h
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2023-10-18
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __APP_FLASH_LOG_H__
#define __APP_FLASH_LOG_H__

/* include ----------------------------------------------------------------*/
#include "app_spi.h"
#include "pin.h"
#include "app_cfg.h"
#include "sys_console.h"
#include <string.h>
#include "app_init.h"
#include "one_wire_bus_app.h"

#define FLASH_LOG_ADDR_STAR         0x30000
#define FLASH_LOG_TIMER_N           50000//300000  //1min

#define  LIST_QUEUE_MAX_BUF_NUM     256 //写入最大长度
#define  LIST_QUEUE_MAX_NUM         300
#define  FLASH_LOG_MAX_PAGE_COUNT   128  //存储log最大页数

#define  FLASH_LOG_STAR_CMD         1
#define  FLASH_LOG_TRA_DATA_CMD     2
#define  FLASH_LOGG_END_CMD         3
#define  FLASH_LOGG_GET_TIMER_CMD   4

typedef struct list_queue
{
    uint16_t head;                  //队列头指针
    uint16_t tail;                  //队列尾指针
    uint16_t list_queue_num;        //队列已有长度
    uint16_t list_queue_end_num;    //队列最大长度
    uint8_t * qu;                   //队列缓存空间    
}ListQueueStrust;

extern ListQueueStrust flash_log_queue;
extern uint8_t global_log_timer_buf[6];

extern void app_flash_log_init(void);
extern void app_flash_log_process(void);
extern void app_flash_log_printf(char * char_buf,uint8_t * data_buf,uint16_t data_len);

uint16_t app_flash_log_find_page(ListQueueStrust * q);
void app_flash_log_erase_addr(ListQueueStrust * q);
bool list_queue_create(ListQueueStrust * q, uint8_t *p_buf, uint16_t list_queue_len);
bool list_queue_write(ListQueueStrust * q, uint8_t * p_in_buf, uint16_t in_data_len);
bool list_queue_read(ListQueueStrust * q, uint8_t * p_out_buf, uint16_t out_data_len); 
uint16_t list_queue_is_full(ListQueueStrust *q);
void hex_to_asciistring(uint8_t* str,uint32_t size,uint8_t* str1);
void app_flash_log_16_to_10(uint8_t * in_buf);
bool app_flash_data_analysis(uint8_t *rx_data, uint8_t *pack);

/**
 * @brief 上报log内容
 * 
 * @param offset 绝对偏移地址
 * @param data_len 获取数据长度
 */
void app_flash_log_pad_get_log(uint16_t offset, uint8_t data_len, uint8_t *read_buf, ListQueueStrust * q);
#endif


/*
***************************************file descriptor**************************************
*    1、void app_flash_log_init(void);初始化                                                *
*       list_queue_create();创建队列                                                        *
*       app_flash_log_timer_tick_0_1_ms_callback()定时器调用                                *
*       app_flash_log_process()                                                           *
*    2、根据上位机命令调用擦除、读取log函数                                                   *
*       app_flash_log_erase_addr(); //擦除                                                 *
*       golbal_get_flash_log_flag //读取标志                                                * 
*    3、使用如下函数写入log                                                                  *
*        void app_flash_log_printf(char * char_buf,uint8_t * data_buf,uint16_t data_len);  *
*        char_buf : 字符串指针                                                              *
*        data_buf ：数组指针                                                                *
*        data_len ：数组长度                                                                * 
*********************************************************************************************
*/


